package com.qhs.sys.controller;

import com.qhs.framework.bean.DataTableView;
import com.qhs.framework.bean.PageView;
import com.qhs.framework.config.shiro.EncryptPwd;
import com.qhs.framework.utils.ModelUtil;
import com.qhs.framework.utils.Result;
import com.qhs.sys.constant.TreeView;
import com.qhs.sys.entity.SysUserInfo;
import com.qhs.sys.service.RoleService;
import com.qhs.sys.service.UserService;
import com.qhs.sys.vo.ChangeVo;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.shiro.crypto.hash.Sha512Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by xinchao on 2017/1/12 0012.
 */
@Controller
@RequestMapping(value = {"user"})
public class UserInfoController {

    private final Logger logger = Logger.getLogger(getClass());

    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;

    /**
     * 查询获取用户列表
     */
    @RequestMapping(value = {"info"})
    public String info(){
        return "sys/user/index3";
    }

    @RequestMapping(value = {"list"})
    @ResponseBody
    public DataTableView<SysUserInfo> searchList2(SysUserInfo sysUserInfo, PageView pageView) {

        return userService.findUserPageList(sysUserInfo,pageView);
    }

    /**
     * 进入新增页面
     * @param sysUserInfo
     * @return
     */

    /**
     * 保存用户信息
     * @param sysUserInfo
     * @return
     */
    @RequestMapping(value = "/edit", method= RequestMethod.POST)
    @ResponseBody
    public Result addUser(SysUserInfo sysUserInfo) {

        String valMes= validateForm(sysUserInfo);
        if(StringUtils.isNotEmpty(valMes)){
           return new Result(valMes);
        }
        if(sysUserInfo.getId()!=null) {
            try {
                ModelUtil.updateInit(sysUserInfo);
                userService.updateByPrimaryKeySelective(sysUserInfo);

            } catch (Exception e) {
                return new Result(false, "保存失败");
            }

        }else{
            SysUserInfo check = userService.findUserByName(sysUserInfo.getUserName());
            if (check != null) {
                return new Result(false, "该账号已经存在，请更换一个");
            }

            try {
                ModelUtil.insertInit(sysUserInfo);
                new EncryptPwd().encryptPassword(sysUserInfo);
                sysUserInfo.setStatus(1);
                userService.insert(sysUserInfo);
            } catch (Exception e) {
                return new Result(false, "保存失败");
            }

        }
        return new Result(true, "操作成功");
    }

    /**
     * 修改密码
     * @param changeVo
     * @return
     */
    @RequestMapping(value = "changePassword",method =RequestMethod.POST )
    @ResponseBody
    public Result changePassword(ChangeVo changeVo){
        SysUserInfo userInfo = null;
        try {
            userInfo = userService.selectByPrimaryKey(changeVo.getUserId());
        } catch (Exception e) {
            return new Result(false,"用户信息获取失败");
        }
        String hex = new Sha512Hash(changeVo.getOldPass(), userInfo.getSalt()).toHex();
        if(hex.equals(userInfo.getPassword())){
            userInfo.setPassword(new Sha512Hash(changeVo.getNewPass(), userInfo.getSalt()).toHex());

            try {
                userService.updateByPrimaryKeySelective(userInfo);
            } catch (Exception e) {
                return new Result(false,"修改密码失败");
            }


        }else{
            return new Result(false,"原密码错误,请重新输入");
        }
        return new Result(true,"修改成功");
    }

    /**
     * 删除用户
     * @param userId
     * @return
     */
    @RequestMapping(value="/delete")
    @ResponseBody
    public Result delete(@RequestParam Long userId){
        try {
            userService.deleteByPrimaryKey(userId);
        } catch (Exception e) {
            return new Result(false,"删除失败");
        }
        return new Result(true,"删除成功");
    }
    /**
     * 获取角色
     */
    @RequestMapping(value="/getRole",method = RequestMethod.POST)
    @ResponseBody
    public Result getRole(@RequestParam String userName){
        List<TreeView> treeView = roleService.getTreeView(userName);
        return new Result(true,"",treeView);
    }
    /**
     * 校验提交信息
     * @param sysUserInfo
     * @return
     */
    private String validateForm(SysUserInfo sysUserInfo){
        String msg = "";
        //Strings.isNullOrEmpty("");
        if(StringUtils.isEmpty(sysUserInfo.getUserName())){
            msg= "该账户名不能为空";
        }
        if(StringUtils.isEmpty(sysUserInfo.getPassword())){
            msg= "密码不能为空";
        }
        if(StringUtils.isEmpty(sysUserInfo.getNickName())){
            msg= "名称不能为空";
        }
        return msg;
    }






}
